"""
辅助功能函数模块
- 依赖核心函数的输入输出格式
"""

import pandas as pd
from tabulate import tabulate

def get_mean_table(df, score_fields, year_field):
    """分组均值统计"""
    return df.groupby(year_field)[score_fields].mean()

def print_mean_table(mean_table):
    print("\n【各年份四项计分均值】")
    print(tabulate(mean_table, headers='keys', tablefmt='github', floatfmt=".2f"))

def get_box_stats(df, score_fields, year_field):
    """分布对比（箱线统计）"""
    stats = {}
    for field in score_fields:
        desc = df.groupby(year_field)[field].describe()[["count", "mean", "std", "min", "25%", "50%", "75%", "max"]]
        stats[field] = desc
    return stats

def print_box_stats(stats):
    print("\n【各年份四项计分分布（箱线统计）】")
    for field, desc in stats.items():
        print(f"\n字段：{field}")
        print(tabulate(desc, headers='keys', tablefmt='github', floatfmt=".2f"))

def anova_by_year(df, score_fields, year_field):
    """方差分析（ANOVA）"""
    from scipy.stats import f_oneway
    results = []
    for field in score_fields:
        groups = [g.dropna().values for name, g in df.groupby(year_field)[field]]
        if all(len(g) > 1 for g in groups):
            fval, pval = f_oneway(*groups)
            results.append([field, f"{fval:.2f}", f"{pval:.4f}"])
        else:
            results.append([field, "数据不足", "数据不足"])
    return results

def print_anova_results(results):
    print("\n【方差分析（ANOVA）结果】")
    print(tabulate(results, headers=["字段", "F值", "p值"], tablefmt='github'))
